package cn.ekuma.impexp.ui.swing;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import java.awt.Dimension;
import java.util.List;
import javax.swing.BoxLayout;
import cn.ekuma.impexp.FieldGenerator;
import cn.ekuma.impexp.generator.GeneratorUtils;
import cn.ekuma.util.Utils;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.format.Formats;
import java.awt.Insets;
import javax.swing.border.EmptyBorder;

public class JColumnMatchPanel extends JPanel {
	private JTextField textField;
	private JTextField txtSrcColName;
	private JTextField txtSrcType;
	private JTextField txtDesCol;
	private JTextField txtDefValue;

	ColumnMatch columnMatch;
	private JCheckBox chckbxNewCheckBox;
	private JComboBox cbGrentte;
	private JCheckBox cbIanage;
	private JPanel panel_2;
	private JPanel panel_3;
	
	/**
	 * Create the panel.
	 */
	public JColumnMatchPanel() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		panel_2 = new JPanel();
		add(panel_2);
		
		textField = new JTextField();
		panel_2.add(textField);
		textField.setEditable(false);
		textField.setColumns(3);
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		add(panel);
		
		JLabel label_1 = new JLabel("字段名");
		panel.add(label_1);
		
		txtSrcColName = new JTextField();
		txtSrcColName.setEditable(false);
		panel.add(txtSrcColName);
		txtSrcColName.setColumns(10);
		
		JLabel label_2 = new JLabel("类型");
		panel.add(label_2);
		
		txtSrcType = new JTextField();
		txtSrcType.setEditable(false);
		panel.add(txtSrcType);
		txtSrcType.setColumns(6);
		
		panel_3 = new JPanel();
		add(panel_3);
		
		JLabel label_3 = new JLabel("映射列");
		panel_3.add(label_3);
		
		txtDesCol = new JTextField();
		panel_3.add(txtDesCol);
		txtDesCol.setColumns(3);
		
		 cbIanage = new JCheckBox("忽略");
		add(cbIanage);
		
		JPanel panel_1 = new JPanel();
		add(panel_1);
		
		JLabel label_4 = new JLabel("值生成器");
		panel_1.add(label_4);
		
		cbGrentte = new JComboBox();
		cbGrentte.setMaximumRowCount(15);
		cbGrentte.setPreferredSize(new Dimension(65, 21));
		panel_1.add(cbGrentte);
		
		JLabel label_5 = new JLabel("默认值");
		panel_1.add(label_5);
		
		txtDefValue = new JTextField();
		panel_1.add(txtDefValue);
		txtDefValue.setColumns(8);
		
	    chckbxNewCheckBox = new JCheckBox("只对空值");
		panel_1.add(chckbxNewCheckBox);
	}

    public void setColumnMatch(ColumnMatch c){
		 columnMatch=c;
		 textField.setText(String.valueOf(c.getSrcCol()));
		 txtSrcColName.setText(c.getColName());
		 txtSrcType.setText(Utils.getDatasShowName(c.getDataType()));
		 txtDesCol.setText(String.valueOf(c.getDestCol())); 
		 List<FieldGenerator> generatorList= GeneratorUtils.getFieldGeneratorListByData(c.getDataType());
		 cbGrentte.addItem(new ComboBoxValEntry(ComboBoxValEntry.TYPE_null,"未选择"));
		 for(FieldGenerator v:generatorList){
			 cbGrentte.addItem(new ComboBoxValEntry(v,v.getName()));
		 }
		 cbIanage.setSelected(c.isIegore());
		 chckbxNewCheckBox.setSelected(c.isJustChangeNullValue());
	}
	
	public ColumnMatch getColumnMatch(){
		try {
			columnMatch.setDestCol((Integer) Formats.INT.parseValue(txtDesCol.getText()));
		} catch (BasicException e) {
			e.printStackTrace();
		}
		columnMatch.setDefValue(txtDefValue.getText());
		columnMatch.setIegore(cbIanage.isSelected());
		columnMatch.setJustChangeNullValue(chckbxNewCheckBox.isSelected());
		ComboBoxValEntry entry=(ComboBoxValEntry) cbGrentte.getSelectedItem();
		if(entry!=null&&entry.getM_sKey()!=ComboBoxValEntry.TYPE_null)
		  columnMatch.setFidldGenerator((FieldGenerator) entry.getKey());
		
		return columnMatch;
	}
	
}
